<?php
class Curl {
    private static $curl_header = array();

    public function __construct()
    {
    }

    public static function  headerCallback($ch, $strHeader){
        $pos = strpos($strHeader, ":");
        if ($pos !== FALSE){
            $name  = substr($strHeader, 0, $pos);
            $value = substr($strHeader, $pos+1);
            $value = str_replace("\r\n", "", $value);
            $value = ltrim($value);
            self::$curl_header[$name] = $value;
        }
        return strlen($strHeader);
    }

    function call($url, $header = FALSE, $quest = FALSE, $postarr = FALSE, $file_path = FALSE)
    {
        $quest = strtoupper($quest);
        xj_log_message('debug','['.__METHOD__.']'." url:". $url);
        xj_log_message('debug','['.__METHOD__.']'." header:". json_encode($header));
        xj_log_message('debug','['.__METHOD__.']'." quest:". $quest);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  // 从证书中检查SSL加密算法是否存在

        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);  //将curl_exec()获取的信息以文件流的形式返回，而不是直接输出。
        if ($header !== FALSE)
            curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch,CURLOPT_HEADERFUNCTION, 'Curl::headerCallback');
        if (!empty($postarr)){
            empty($quest) && $quest = 'POST';
            curl_setopt($ch, CURLOPT_POST, true);
            if (is_array($postarr)){
                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postarr));
                xj_log_message('debug','['.__METHOD__.']'." post:". json_encode($postarr));
            }else{
                curl_setopt($ch, CURLOPT_POSTFIELDS, strval($postarr));
                xj_log_message('debug','['.__METHOD__.']'." post:". (string)$postarr);
            }
        }
        $fd = FALSE;
        if (!empty($file_path) and strcmp($quest,'PUT') == 0){
           xj_log_message('debug','['.__METHOD__.']'." put filepath:". $file_path);
            $fd = fopen($file_path, 'r');
            if ($fd === FALSE){
               xj_log_message('error','['.__METHOD__.']'." fopen error.file_path:". $file_path);
                curl_close($ch);
                return FALSE;
            }
            curl_setopt($ch, CURLOPT_PUT, 1);
            curl_setopt($ch, CURLOPT_INFILE, $fd);
            curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path));
            $timeout = filesize($file_path)/1000;
            if ($timeout < 100){
                $timeout = 100;
            }
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
            curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        }
        //var_dump(empty($file_path));
        if (!empty($file_path) and strcmp($quest,'GET') == 0){
           xj_log_message('debug','['.__METHOD__.']'." get filepath:". $file_path);
            if (file_exists($file_path)){
                @unlink($file_path);
            }
            $fd = @fopen($file_path, 'w');
            if ($fd === FALSE){
               xj_log_message('error','['.__METHOD__.']'." fopen error.file_path:". $file_path);
                curl_close($ch);
                return FALSE;
            }
            curl_setopt($ch, CURLOPT_FILE, $fd);
        }
        if (strcmp($quest,'HEAD') == 0){
            curl_setopt($ch, CURLOPT_NOBODY, true);
        }
        empty($quest) && $quest = 'GET';
        curl_setopt($ch,CURLOPT_CUSTOMREQUEST, $quest);
        $content = curl_exec($ch);
        if ($fd !== FALSE){
            fflush($fd);
            fclose($fd);
        }
        if(curl_errno($ch)){
           xj_log_message('error','['.__METHOD__.']'." curl_error:". curl_error($ch));
            curl_close($ch);
            return FALSE;
        }
        $info =  curl_getinfo($ch);
        curl_close($ch);
       xj_log_message('debug','['.__METHOD__.']'. " http_code:".(string)$info["http_code"]."");
       xj_log_message('debug','['.__METHOD__.']'. " header:".json_encode(self::$curl_header));
       xj_log_message('debug','['.__METHOD__.']'. " html:".$content);
        $curl_return = array();
        $curl_return['url'] = $url;
        $curl_return['code'] = $info["http_code"];
        $curl_return['header'] = self::$curl_header;
        $curl_return['html'] = $content;
        return $curl_return;
    }
}
